package com.example.hotHub.service.impl;

import com.example.hotHub.model.NormalHotSearch;
import com.example.hotHub.service.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

@Slf4j
@Service
public class HotSearchWrapperServiceImpl implements HotSearchWrapperService {

    @Autowired
    private ExecutorService threadPoolExecutor;

    @Autowired
    private BaiduHotPointService baiduHotPointService;

    @Autowired
    private WeiboHotPointService weiboHotPointService;

    @Autowired
    private ToutiaoHotPointService toutiaoHotPointService;

    @Autowired
    private ZhihuHotPointService zhihuHotPointService;

    @Autowired
    private TencentHotPointService tencentHotPointService;

    @Autowired
    private BiliHotPointService biliHotPointService;

    @Autowired
    private HaokanHotPointService haokanHotPointService;

    @Override
    public List<NormalHotSearch> getBaiduHotSearchList() {
        return baiduHotPointService.getHotPointList();
    }

    @Override
    public List<NormalHotSearch> getWeiboHotSearchList() {
        return weiboHotPointService.getHotPointList();
    }

    @Override
    public List<NormalHotSearch> getToutiaoHotSearchList() {
        return toutiaoHotPointService.getHotPointList();
    }

    @SneakyThrows
    @Override
    public List<NormalHotSearch> getZhihuHotSearchList() {
        return zhihuHotPointService.getHotPointList();
    }

    @Override
    public List<NormalHotSearch> getTencentHotSearchList() {
        return tencentHotPointService.getHotPointList();
    }

    /**
     * bilibili
     * @param type
     * @return
     */
    @SneakyThrows
    @Override
    public List<NormalHotSearch> getBiliHotSearchList(String type) {
        return biliHotPointService.getHotPointList(type);
    }


    /**
     * haokan
     * @param
     * @return
     */
    @SneakyThrows
    @Override
    public List<NormalHotSearch> getHaokanHotSearchList() {
        return haokanHotPointService.getHotPointList();
    }

    /**
     * 澎湃
     * @param day
     * @return
     */
    @Deprecated
    @SneakyThrows
    @Override
    public List<NormalHotSearch> getPaperHotSearchList(Integer day) {

        List<NormalHotSearch> normalHotSearchList = new ArrayList<>();

        String elementId = null;

        switch (day) {
            case 1:
                elementId = "listhot0";
                break;
            case 3:
                elementId = "listhot1";
                break;
            case 7:
                elementId = "listhot2";
                break;
        }

        Document document = Jsoup.connect("https://www.thepaper.cn/").get();

        List<Element> elementList = document.getElementsByClass("index_ppreport__slNZB index_notranstion__R0Uwz");

//        String hotPointJSONStr = elementList.html();
//
//        SimpleMap hotPointJSON = SimpleMap.parse(hotPointJSONStr, SimpleMap.class)
//                .getMap("props")
//                .getMap("pageProps")
//                .getMap("data");
//
//        List<SimpleMap> list = hotPointJSON
//                .getList("recommendImg", SimpleMap.class);
//
//        Elements hotList = elementList.select("main index_headerfixed__GyBYK");// div.container div div.index_flex__ee6jS div.index_content___Uhtm div.index_commonsider__3D368 div.index_ppreport__slNZB index_notranstion__R0Uwz div.index_content___Uhtm ul li a");

//        for (Element element : hotList) {
//            String title = element.text();
//            String url = element.attr("href");
//            NormalHotSearch normalHotSearch = new NormalHotSearch()
//                .setTitle(title)
//                .setUrl("https://www.thepaper.cn/" + url);
//            normalHotSearchList.add(normalHotSearch);
//        }

        normalHotSearchList = normalHotSearchList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

//        log.info("成功爬取澎湃热点({}天内): {}", day, normalHotSearchList);

        return normalHotSearchList;
    }


    /**
     * 52破解
     * @param view
     * @param day
     * @return
     */
    @SneakyThrows
    @Override
    public List<NormalHotSearch> get52PoJieHotSearchList(String view, Integer day) {

        List<NormalHotSearch> normalHotSearchList = new ArrayList<>();

        String orderby = null;

        switch (day) {
            case 1:
                orderby = "today";
                break;
            case 7:
                orderby = "thisweek";
                break;
            case 30:
                orderby = "thismonth";
                break;
            case -1:
                orderby = "all";
        }

        String target = "https://www.52pojie.cn/misc.php?mod=ranklist&type=thread&view=" + view + "&orderby=" + orderby;
//        log.info("wrapper target: {}", target);
        Document document = Jsoup.connect(target).get();
        Element element = document.getElementsByClass("tl").get(0);
        Element table = element.select("table").get(0);
        Elements trList = element.select("table tbody").get(1).select("tr"); //table.select("tr");
        for (Element tr : trList) {
            Element th = tr.select("th").get(0);
            String url = "https://www.52pojie.cn/" + th.select("a").get(0).attr("href");
            String title = th.text();
            Elements tdList = tr.select("td");
            String hotValue = tdList.get(tdList.size() - 1).select("a").get(0).text();
            NormalHotSearch normalHotSearch = new NormalHotSearch()
                .setTitle(title)
                .setUrl(url)
                .setHotValue(Integer.parseInt(hotValue));
            normalHotSearchList.add(normalHotSearch);
        }

        normalHotSearchList = normalHotSearchList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

//        log.info("成功爬取吾爱破解热点({}天内): {}", day, normalHotSearchList);

        return normalHotSearchList;
    }

}
